package ping
import(
	"iot-base/common/cache"
	"github.com/go-ping/ping"
	"time"
	"fmt"
)
func PingThing(ip string,id string){
	go pingthing(ip,id)
}

func pingthing(ip string,id string) (bool,*ping.Statistics){
	
	pinger, err := ping.NewPinger(ip)
	if(err != nil){
		//cache.SetLastAliveTime(id,now)
		fmt.Println("--------ping----1-----",err)
		return false,nil
	}
	pinger.SetPrivileged(true)
	pinger.Count = 1
	pinger.Timeout = time.Second * 2
	pinger.Interval  = time.Second
	err = pinger.Run() 
	if(err != nil){
		//cache.SetLastAliveTime(id,now)
		fmt.Println("--------ping----2-----",err)
		return false,nil
	}
	stat := pinger.Statistics()
	fmt.Println("--------ping-----3----",stat)
	if(stat.PacketsRecv == 0){
		//cache.SetLastAliveTime(id,now)
		
		return false,stat
	}
	
	//now := time.Now().Unix()
	cache.SetLastAliveTime(id,time.Now().Unix())
	return true,stat
}